<?php
// based on Gtk2_FileDrop from Christian Weiske <cweiske@php.net>
class Notepad_FileDrop {
	protected $widget = null;
	protected $callback = null;
	public static function attach(GtkWidget $widget, $callback) {
		$widget->drag_dest_set(Gtk::DEST_DEFAULT_ALL, array(array('text/uri-list', 0, 0)), Gdk::ACTION_COPY | Gdk::ACTION_MOVE);
		$instance = new Notepad_FileDrop($callback);
		$widget->connect('drag-data-received', array($instance, 'dragDataReceived'));
		//$instance->connectChildren($widget);
		return true;
	}
	/*protected function connectChildren($widget) {
		if(!method_exists($widget, 'get_children'))
			return false;
		$children = $widget->get_children();
		foreach($children as $child) {
			if(method_exists($child, 'connect'))
				$child->connect('drag-data-received', array($this, 'dragDataReceived'));
			$this->connectChildren($child);
		}
		return true;
	}*/
	protected function __construct($callback) {
		$this->callback = $callback;
	}
    public function dragDataReceived(GtkWidget $widget, GdkDragContext $context , $x, $y, $data , $info, $time) {
        $arData = explode("\n", $data->data);
        $files = array();
        foreach ($arData as $strLine) {
            $strLine = trim($strLine);
            if ($strLine == '') {
                continue;
            }
            $file = self::getPathFromUrilistEntry($strLine);
            if($path = realpath($file) and !is_dir($file))
            	$files[] = $file;
        }
        call_user_func($this->callback, $widget, $files);
        return true;
    }
    private static function getPathFromUrilistEntry($strUriFile) {
        $strUriFile = urldecode($strUriFile);
        $bUrl = false;
        if (substr($strUriFile, 0, 5) == 'file:') {
            if (substr($strUriFile, 0, 17) == 'file://localhost/') {
                $strFile = substr($strUriFile, 16);
            } else if (substr($strUriFile, 0, 8) == 'file:///') {
                $strFile = substr($strUriFile, 7);
            } else if ($strUriFile[5] == '/') {
                $strUriFile = substr($strUriFile, 5);
                for( $n = 1; $n < 5; $n++) {
                    if ($strUriFile[$n] != '/') {
                        break;
                    }
                }
                $strUriFile = substr($strUriFile, $n - 1);
                if (!file_exists($strUriFile)) {
                    $strUriFileNoHost = strstr(substr($strUriFile, 1), '/');
                    if (file_exists($strUriFileNoHost)) {
                        $strUriFile = $strUriFileNoHost;
                    }
                }
                $strFile = $strUriFile;
            } else {
                $strFile = substr ($strUriFile, 5);
            }
        } else if (strstr($strUriFile, '://')) {
            $strFile = $strUriFile;
            $bUrl    = true;
        } else {
            $strFile = $strUriFile;
        }
        if (!$bUrl && $strFile[2] == ':' && $strFile[0] == '/') {
            $strFile = str_replace('/', '\\', substr($strFile, 1));
        }
        return $strFile;
    }
}
?>